package com.sds.sweng.swat.miplatform.ui.design.editors;

import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.ui.parts.GraphicalViewerKeyHandler;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;

import com.sds.sweng.swat.miplatform.ui.design.dialogs.AddFavoriteDialog;
import com.sds.sweng.swat.miplatform.ui.design.models.MiplatformComponent;
import com.sds.sweng.swat.miplatform.ui.design.models.MiplatformDesignModel;
import com.sds.sweng.swat.miplatform.ui.design.models.area.AreaTemplate;
import com.sds.sweng.swat.miplatform.ui.design.models.area.sub.SubArea;
import com.sds.sweng.swat.miplatform.ui.design.parts.area.AreaTemplatePart;
import com.sds.sweng.swat.miplatform.ui.design.parts.area.sub.SubAreaPart;

public class ExtentedKeyHandler extends GraphicalViewerKeyHandler {

	public ExtentedKeyHandler(GraphicalViewer viewer) {
		super(viewer);
	}

	public boolean keyPressed(KeyEvent event) {
		if (event.keyCode == SWT.F4) {
			if (getFocusEditPart() instanceof AreaTemplatePart || getFocusEditPart() instanceof SubAreaPart) {
				if (getFocusEditPart() instanceof SubAreaPart) {
					if (((SubArea) getFocusEditPart().getModel()).isEmpty())
						return super.keyPressed(event);
				} else if (getFocusEditPart() instanceof AreaTemplatePart) {
					int width = ((MiplatformDesignModel) getFocusEditPart().getParent().getModel()).getContentsWidth();
					((AreaTemplate) getFocusEditPart().getModel()).setWidth(width);
				}

				AddFavoriteDialog dialog = new AddFavoriteDialog((MiplatformComponent) getFocusEditPart().getModel());
				dialog.open();
			}
		}

	    return super.keyPressed(event);

	}

}
